"""
Disclaimer
==========

This software was developed at the National Institute of Standards and Technology at the NIST Center for Neutron Research by employees of the Federal Government in the course of their official duties. Pursuant to title 17 section 105* of the United States Code this software is not subject to copyright protection and is in the public domain. The SPINAL software package is an experimental spinwave analysis system. NIST assumes no responsibility whatsoever for its use, and makes no guarantees, expressed or implied, about its quality, reliability, or any other characteristic. The use of certain trade names or commercial products does not imply any endorsement of a particular product, nor does it imply that the named product is necessarily the best product for the stated purpose. We would appreciate acknowledgment if the software is used.

*Subject matter of copyright: United States Government works

Copyright protection under this title is not available for any work of the United States Government, but the United States Government is not precluded from receiving and holding copyrights transferred to it by assignment, bequest, or otherwise."""


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Mon Mar 15 20:43:40 2010

import wx
#from spinwaves.vtkModel.AtomClass import color_dict, radius_dict
import spinwaves.vtkModel.AtomClass as AtomClass
import copy


# begin wxGlade: extracode
# end wxGlade

def openAtomDisplayPref(Element_symbol, parent = None):
	Atom_pref_frame = AtomPrefFrame(Element_symbol, parent, -1, "")
	Atom_pref_frame.Show()


class AtomPrefFrame(wx.Frame):
	def __init__(self, symbol = None, *args, **kwds):
		# begin wxGlade: AtomPrefFrame.__init__
		kwds["style"] = wx.DEFAULT_FRAME_STYLE
		wx.Frame.__init__(self, *args, **kwds)

		self.temp_color_dict = copy.deepcopy(AtomClass.color_dict)
		self.temp_radius_dict = copy.deepcopy(AtomClass.radius_dict)

		self.atom_pref_panel = wx.Panel(self, -1)
		self.atom_label = wx.StaticText(self.atom_pref_panel, -1, "Atom: ")
		self.el_combo_box = wx.ComboBox(self.atom_pref_panel, -1, choices=self.temp_color_dict.keys(), style=wx.CB_SIMPLE)
		self.radius_label = wx.StaticText(self.atom_pref_panel, -1, "Radius: ")
		self.radius_text_ctrl = wx.TextCtrl(self.atom_pref_panel, -1, "")
		self.rad_unit_label = wx.StaticText(self.atom_pref_panel, -1, u"\u212B")
		self.color_label = wx.StaticText(self.atom_pref_panel, -1, "Color: ")
		self.r_spin_ctrl = wx.SpinCtrl(self.atom_pref_panel, -1, "", min=0, max=255)
		self.g_spin_ctrl = wx.SpinCtrl(self.atom_pref_panel, -1, "", min=0, max=255)
		self.b_spin_ctrl = wx.SpinCtrl(self.atom_pref_panel, -1, "", min=0, max=255)
		self.colored_panel = wx.Panel(self.atom_pref_panel, -1, style=wx.SIMPLE_BORDER)
		self.color_choose_button = wx.Button(self.atom_pref_panel, -1, "Select")
		self.ok_button = wx.Button(self.atom_pref_panel, -1, "Ok")
		self.cancel_button = wx.Button(self.atom_pref_panel, -1, "Cancel")

		self.__set_properties()
		self.__do_layout()

		self.Bind(wx.EVT_COMBOBOX, self.OnAtomChange, self.el_combo_box)
		#Every time the text here changes it will be validated and used
		self.Bind(wx.EVT_TEXT, self.OnRadiusChange, self.radius_text_ctrl)
		self.Bind(wx.EVT_SPINCTRL, self.OnColorChange, self.r_spin_ctrl)
		self.Bind(wx.EVT_SPINCTRL, self.OnColorChange, self.g_spin_ctrl)
		self.Bind(wx.EVT_SPINCTRL, self.OnColorChange, self.b_spin_ctrl)
		self.Bind(wx.EVT_BUTTON, self.OnOk, self.ok_button)
		self.Bind(wx.EVT_BUTTON, self.OnCancel, self.cancel_button)
		# end wxGlade
		self.Bind(wx.EVT_TEXT, self.OnElementType, self.el_combo_box)
		self.Bind(wx.EVT_BUTTON, self.OnColorSelect, self.color_choose_button)
		if symbol != None:
			self._setElement(symbol)

		#Background color to change fields which recieved invalid values
		self.bgColor = "pink"


	def __set_properties(self):
		# begin wxGlade: AtomPrefFrame.__set_properties
		self.SetTitle("Atom Display Preferences")
		self.colored_panel.SetMinSize((80, 25))
		self.colored_panel.SetBackgroundColour(wx.Colour(176, 0, 255))
		self.colored_panel.Enable(False)
		# end wxGlade

	def __do_layout(self):
		# begin wxGlade: AtomPrefFrame.__do_layout
		sizer_1 = wx.BoxSizer(wx.VERTICAL)
		sizer_2 = wx.BoxSizer(wx.VERTICAL)
		sizer_8 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_6.Add(self.atom_label, 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_6.Add(self.el_combo_box, 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_6.Add((25, 50), 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_6.Add(self.radius_label, 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_6.Add(self.radius_text_ctrl, 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_6.Add(self.rad_unit_label, 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_2.Add(sizer_6, 1, wx.EXPAND, 0)
		sizer_7.Add(self.color_label, 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_7.Add(self.r_spin_ctrl, 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_7.Add(self.g_spin_ctrl, 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_7.Add(self.b_spin_ctrl, 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_7.Add(self.colored_panel, 1, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_7.Add(self.color_choose_button, 0, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_2.Add(sizer_7, 1, wx.EXPAND, 0)
		sizer_8.Add(self.ok_button, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_8.Add(self.cancel_button, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_2.Add(sizer_8, 1, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
		self.atom_pref_panel.SetSizer(sizer_2)
		sizer_1.Add(self.atom_pref_panel, 0, wx.EXPAND, 3)
		self.SetSizer(sizer_1)
		sizer_1.Fit(self)
		self.Layout()
		# end wxGlade

	def OnAtomChange(self, event): # wxGlade: AtomPrefFrame.<event_handler>
		self._setElement(self.el_combo_box.GetValue())
		event.Skip()

	def OnRadiusChange(self, event): # wxGlade: AtomPrefFrame.<event_handler>
		#Validate radius(must be a float)
		num = None
		try:
			num = float(self.radius_text_ctrl.GetValue())
			self.radius_text_ctrl.SetBackgroundColour("white")
			self.temp_radius_dict[self.el_combo_box.GetValue()] = num
		except:
			self.radius_text_ctrl.SetBackgroundColour(self.bgColor)
		event.Skip()

	def OnColorChange(self, event): # wxGlade: AtomPrefFrame.<event_handler>
		#The color settings are immediately updated when a change is made
		element = self.el_combo_box.GetValue()
		color = [self.r_spin_ctrl.GetValue(), self.g_spin_ctrl.GetValue(), self.b_spin_ctrl.GetValue()]
		self.temp_color_dict[element] = color
		self.colored_panel.SetBackgroundColour(color)
		event.Skip()

	def OnColorSelect(self, event):
		"""Opens a color selection dialog."""
		#This could also have been done with a PyColourChooser, which has a different feel

		#Taken from wx demo and modified
		dlg = wx.ColourDialog(self)

		# Ensure the full colour dialog is displayed, 
		# not the abbreviated version.
		dlg.GetColourData().SetChooseFull(True)

		if dlg.ShowModal() == wx.ID_OK:

			# If the user selected OK, then the dialog's wx.ColourData will
			# contain valid information. Fetch the data ...
			data = dlg.GetColourData()

			# ... then do something with it. The actual colour data will be
			# returned as a three-tuple (r, g, b) in this particular case.
			data = data.GetColour()
			self.r_spin_ctrl.SetValue(data[0])
			self.g_spin_ctrl.SetValue(data[1])
			self.b_spin_ctrl.SetValue(data[2])
			self.colored_panel.SetBackgroundColour(data)
			element = self.el_combo_box.GetValue()
			self.temp_color_dict[element] = data

		# Once the dialog is destroyed, Mr. wx.ColourData is no longer your
		# friend. Don't use it again!
		dlg.Destroy()

		event.Skip()

	def OnOk(self, event): # wxGlade: AtomPrefFrame.<event_handler>
		AtomClass.color_dict = self.temp_color_dict
		AtomClass.radius_dict = self.temp_radius_dict
		self.Destroy()
		print "On Ok Not completely implemented yet!"
		print "Make sure to rerender!"
		event.Skip()

	def OnCancel(self, event): # wxGlade: AtomPrefFrame.<event_handler>
		self.Destroy()
		event.Skip()

	def OnElementType(self, event):
		"""When the user types something in the element combo box, it will be validated
		to make sure that it is an element that is in the list or the beginning of an
		element symbol."""

		text = self.el_combo_box.GetValue()
		print "text: ", text
		if self.temp_color_dict.keys().count(text) < 1:#The current entry does not correspond to an element symbol
			for el in self.temp_color_dict.keys():
				#If there exists an element which begins with the current text
				if text == el[0:len(text)]:
					#self.el_combo_box.SetStringSelection(el)
					#self.el_combo_box.SetMark(len(text),len(el))
					break #The first match will be used
			else:#There are no elements that begin with that character string
				self.el_combo_box.SetValue(text[0:len(text)-1])#remove the character
		else:#The current text is a valid element symbol
			self._setElement(text)
		event.Skip()

	def _setElement(self, element_symbol):
		self.el_combo_box.SetValue(element_symbol)
		self.radius_text_ctrl.SetValue(str(self.temp_radius_dict[element_symbol]))
		color = self.temp_color_dict[element_symbol]
		self.r_spin_ctrl.SetValue(color[0])
		self.g_spin_ctrl.SetValue(color[1])
		self.b_spin_ctrl.SetValue(color[2])
		self.colored_panel.SetBackgroundColour(wx.Colour(color[0],color[1],color[2]))

# end of class AtomPrefFrame


if __name__ == "__main__":
	app = wx.PySimpleApp(0)
	wx.InitAllImageHandlers()
	Atom_pref_frame = AtomPrefFrame(None, -1, "")
	app.SetTopWindow(Atom_pref_frame)
	Atom_pref_frame.Show()
	app.MainLoop()
